package com.ld.service;

import com.ld.dao.DBUtilDao;
import com.ld.dao.FilmDao;
import com.ld.entity.Film;
import com.ld.entity.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("filmService")
@Transactional
public class FilmServiceImpl implements FilmService {
    @Resource(name = "filmDao")
    private FilmDao filmDao;

    @Resource(name = "DBUtilDao")
    private DBUtilDao dbUtilDao;

    /**
     *
     * @param pageSize 页面大小
     * @param currentPage 当前页
     * @return
     */
    @Transactional
    public Map<String,Object> queryAllFilm(int currentPage,int pageSize){
        int totalCount = getTabCount("film");//总条数
        PageInfo pageInfo =new PageInfo();
        pageInfo.setTotalCount(totalCount);
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrentPage(currentPage);
        int startPage= (currentPage-1)*pageSize;//mysql从0开始算
        List<Film> films = filmDao.queryAllFilm(startPage, pageSize);
        Map filmMap=new HashMap();
        filmMap.put("films",films);
        filmMap.put("pageInfo",pageInfo);
        return filmMap;
    }

    /**
     *
     * @param tabName:要查的表名
     * @return:这张表共几条数据
     */
    @Transactional
    public int getTabCount(String tabName){
        return dbUtilDao.getTabCount(tabName);
    }


}
